//
//  MusicView.m
//  哎游
//
//  Created by 康守帅 on 15-1-7.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "MusicView.h"

static CGFloat const backGroundHeight = 200.0f;

@interface MusicView()

@property (retain, nonatomic) UIView *backGroundView;
@property (retain, nonatomic) UIView *foreGroundView;
@property (retain, nonatomic) UIScrollView *backGroundScrollView;
@property (retain, nonatomic) UIScrollView *foreGroundScrollView;

- (void)updateBackGroundFrame;
- (void)updateForeGroundFrame;
- (void)updateContentOffset;

@end

@implementation MusicView

- (instancetype)initWithBackGroundView:(UIView *)backGroundView foreGroundView:(UIView *)foreGroundView
{
    self = [super init];
    if (self) {
        self.backGroundHeight = backGroundHeight;
        self.backGroundView = backGroundView;
        self.foreGroundView = foreGroundView;
        
        // 添加背景视图
        self.backGroundScrollView = [[UIScrollView alloc] init];
        _backGroundScrollView.backgroundColor = [UIColor clearColor];
        // 是否显示水平方向滚动条
        _backGroundScrollView.showsHorizontalScrollIndicator = NO;
        // 是否显示竖直方向滚动条
        _backGroundScrollView.showsVerticalScrollIndicator = NO;
        [_backGroundScrollView addSubview:_backGroundView];
        [self addSubview:_backGroundScrollView];
        [_backGroundScrollView release];
        
        // 添加前视图
        self.foreGroundScrollView = [[UIScrollView alloc] init];
        _foreGroundScrollView.backgroundColor = [UIColor clearColor];
        _foreGroundScrollView.delegate = self;
        [_foreGroundScrollView addSubview:_foreGroundView];
        [self addSubview:_foreGroundScrollView];
        [_foreGroundScrollView release];
    }
    return self;
}

// 重写视图frame
- (void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
    [self updateBackGroundFrame];
    [self updateForeGroundFrame];
    [self updateContentOffset];
}

// scroll结束滚动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self updateContentOffset];
    if ([self.delegate respondsToSelector:_cmd]) {
        [self.delegate scrollViewDidScroll:scrollView];
    }
}

// scroll开始减速
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
    if ([self.delegate respondsToSelector:_cmd]) {
        [self.delegate scrollViewWillBeginDecelerating:scrollView];
    }
}

// scroll结束减速
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if ([self.delegate respondsToSelector:_cmd]) {
        [self.delegate scrollViewDidEndDecelerating:scrollView];
    }
}

// scroll开始拖拽
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    if ([self.delegate respondsToSelector:_cmd]) {
        [self.delegate scrollViewWillBeginDragging:scrollView];
    }
}

// scroll将要结束拖拽
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
    if ([self.delegate respondsToSelector:_cmd]) {
        [self.delegate scrollViewWillEndDragging:scrollView withVelocity:velocity targetContentOffset:targetContentOffset];
    }
}

// scroll结束拖拽
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    if ([self.delegate respondsToSelector:_cmd]) {
        [self.delegate scrollViewDidEndDragging:scrollView willDecelerate:decelerate];
    }
}


// scroll结束滚动动画
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    if ([self.delegate respondsToSelector:_cmd]) {
        [self.delegate scrollViewDidEndScrollingAnimation:scrollView];
    }
}

// scroll开始缩放
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view {
    if ([self.delegate respondsToSelector:_cmd]) {
        [self.delegate scrollViewWillBeginZooming:scrollView withView:view];
    }
}

// scroll结束缩放
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(double)scale {
    if ([self.delegate respondsToSelector:_cmd]) {
        [self.delegate scrollViewDidEndZooming:scrollView withView:view atScale:scale];
    }
}

- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
    if ([self.delegate respondsToSelector:_cmd]) {
        [self.delegate scrollViewDidZoom:scrollView];
    }
}

- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView {
    if ([self.delegate respondsToSelector:_cmd]) {
        return [self.delegate scrollViewShouldScrollToTop:scrollView];
    }
    return YES;
}

- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView {
    if ([self.delegate respondsToSelector:_cmd]) {
        [self.delegate scrollViewDidScrollToTop:scrollView];
    }
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    if ([self.delegate respondsToSelector:_cmd]) {
        return [self.delegate viewForZoomingInScrollView:scrollView];
    }
    return nil;
}

#pragma mark - Public Interface

- (UIScrollView *)scrollView {
    return self.foreGroundScrollView;
}

- (void)setBackgroundHeight:(CGFloat)backGroundHeight {
    _backGroundHeight = backGroundHeight;
    [self updateBackGroundFrame];
    [self updateForeGroundFrame];
    [self updateContentOffset];
}


#pragma mark - Internal Methods

- (void)updateBackGroundFrame {
    self.backGroundScrollView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    self.backGroundScrollView.contentSize = CGSizeMake(self.frame.size.width, self.frame.size.height);
    self.backGroundScrollView.contentOffset	= CGPointZero;
    self.backGroundView.frame =
    CGRectMake(0, -20, self.frame.size.width,self.backGroundView.frame.size.height);
//    self.backGroundView.frame =
//    CGRectMake(0.0f, floorf((self.backGroundHeight - self.backGroundView.frame.size.height)/2), self.frame.size.width,self.backGroundView.frame.size.height);
}

- (void)updateForeGroundFrame {
    self.foreGroundView.frame = CGRectMake(0.0f,self.backGroundHeight,self.foreGroundView.frame.size.width,self.foreGroundView.frame.size.height);
    
    self.foreGroundScrollView.frame = self.bounds;
    self.foreGroundScrollView.contentSize =
    CGSizeMake(self.foreGroundView.frame.size.width,self.foreGroundView.frame.size.height + self.backGroundHeight);
}

- (void)updateContentOffset {
    CGFloat offsetY   = self.foreGroundScrollView.contentOffset.y;
    CGFloat threshold = self.backGroundView.frame.size.height - self.backGroundHeight;
    
    if (offsetY > -threshold && offsetY < 0.0f) {
//        self.backGroundScrollView.contentOffset = CGPointMake(0.0f, 0);
        self.backGroundScrollView.contentOffset = CGPointMake(0.0f, floorf(offsetY/2));
    } else if (offsetY < 0.0f) {
        self.backGroundScrollView.contentOffset = CGPointMake(0.0f, 0);
//        self.backGroundScrollView.contentOffset = CGPointMake(0.0f, offsetY + floorf(threshold/2));
    } else {
        self.backGroundScrollView.contentOffset = CGPointMake(0.0f, offsetY);
    }
}






/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
